package com.kuaihuoyun.ctms.api;

import com.kuaihuoyun.ctms.dto.*;
import com.kuaihuoyun.ctms.entity.OrderDto;
import com.kuaihuoyun.ctms.entity.OrderEntity;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.POST;
import retrofit2.http.Query;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by rock on 17/3/29.
 */
public class OrderService extends BaseService {

    public OrderService(String url, String token) {
        super(url, token);
    }

    public CtmsResponse<List<String>> createOrder(List<OrderEntity> orderList) throws IOException {
        Call<CtmsResponse<List<String>>> responseCall =
                retrofit.create(OrderApi.class).createOrder(token,
                        CreateOrderRequest.builder().orderList(orderList).build());
        final Response<CtmsResponse<List<String>>> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse<CreateOrderPackResult> createOrderPack(List<OrderEntity> orderList, String driverPhone) throws IOException {
        Call<CtmsResponse<CreateOrderPackResult>> responseCall = retrofit.create(OrderApi.class).createOrderPack(token,
                CreateOrderPackRequest.builder().orderList(orderList).driverPhone(driverPhone).build());
        final Response<CtmsResponse<CreateOrderPackResult>> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse updateOrderBatch(List<OrderEntity> orderList) throws IOException {
        Call<CtmsResponse> responseCall = retrofit.create(OrderApi.class).updateOrderBatch(token,
                UpdateOrderPackRequest.builder().orderList(orderList).build());
        final Response<CtmsResponse> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse<Boolean> cancelOrders(List<String> numbers) throws IOException {
        Call<CtmsResponse<Boolean>> responseCall = retrofit.create(OrderApi.class).cancelOrders(token,
                CancelOrderRequest.builder().numbers(numbers.stream().collect(Collectors.joining(","))).build());
        final Response<CtmsResponse<Boolean>> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse subscribe(List<Integer> status, String callbackUrl) throws IOException {
        Call<CtmsResponse> responseCall = retrofit.create(OrderApi.class).subscribe(token,
                SubscribeRequest.builder()
                        .states(Optional.ofNullable(status.stream().map(String::valueOf).collect(Collectors.joining(","))).orElse(null))
                        .callbackUrl(callbackUrl)
                        .version("v1")
                        .build()
        );
        final Response<CtmsResponse> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse unsubscribe() throws IOException {
        Call<CtmsResponse> responseCall = retrofit.create(OrderApi.class).unsubscribe(token);
        final Response<CtmsResponse> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse<List<OrderDto>> findOrder(String orderNumber, String phoneNumber, String customerNumber) throws IOException {
        return findOrder(FindOrderRequest.builder().orderNumber(orderNumber).phoneNumber(phoneNumber).customerNumber(customerNumber).build());
    }

    public CtmsResponse<List<OrderDto>> findOrder(FindOrderRequest request) throws IOException {
        Call<CtmsResponse<List<OrderDto>>> responseCall = retrofit.create(OrderApi.class).findOrder(token, request);
        final Response<CtmsResponse<List<OrderDto>>> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    public CtmsResponse<FindOrderResult> queryAllOrders(Integer state, Integer page, Integer size) throws IOException {
        return queryAllOrders(QueryAllOrderRequest.builder().state(state).page(page).size(size).build());
    }

    public CtmsResponse<FindOrderResult> queryAllOrders(QueryAllOrderRequest request) throws IOException {
        Call<CtmsResponse<FindOrderResult>> responseCall = retrofit.create(OrderApi.class).queryAllOrders(token, request);
        final Response<CtmsResponse<FindOrderResult>> response = responseCall.execute();
        if(!response.isSuccessful()) {
            return CtmsResponse.create(response);
        }

        return response.body();
    }

    interface OrderApi {

        @POST("/order/create_order")
        Call<CtmsResponse<List<String>>> createOrder(@Query("access_token") String token, @Body CreateOrderRequest request) throws IOException;

        @POST("/order/create_order_pack")
        Call<CtmsResponse<CreateOrderPackResult>> createOrderPack(@Query("access_token") String token, @Body CreateOrderPackRequest request) throws IOException;

        @POST("/order/cancel_orders/v2")
        Call<CtmsResponse<Boolean>> cancelOrders(@Query("access_token") String token, @Body CancelOrderRequest request) throws IOException;

        @POST("/order/subscribe/v2")
        Call<CtmsResponse> subscribe(@Query("access_token") String token, @Body SubscribeRequest request) throws IOException;

        @POST("/order/unsubscribe")
        Call<CtmsResponse> unsubscribe(@Query("access_token") String token) throws IOException;

        @POST("/order/update_batch")
        Call<CtmsResponse> updateOrderBatch(@Query("access_token") String token,  @Body UpdateOrderPackRequest request) throws IOException;

        @POST("/order/find_order")
        Call<CtmsResponse<List<OrderDto>>> findOrder(@Query("access_token") String token, @Body FindOrderRequest request ) throws IOException;

        @POST("/order/query_orders")
        Call<CtmsResponse<FindOrderResult>> queryAllOrders(@Query("access_token") String token, @Body QueryAllOrderRequest request) throws IOException;



    }
}
